package com.example.async_demo.sync;

import com.sun.org.apache.bcel.internal.generic.NEW;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;

/**
 * <P> @Author: SongKeCheng
 * <P> Type: Class
 * <P> Description:
 * <P> Date: 2024/6/4:8:54
 */

@SuppressWarnings({"unchecked", ""})
public class AsSync {
    private static final Logger log = LoggerFactory.getLogger(AsSync.class);
    static Map<Integer, String> userMap = new HashMap<Integer, String>() {
        {
            put(1, "xm");
            put(2, "xh");
            put(3, "zs");
        }
    };

    static Map<Integer, String> addressMap = new HashMap<Integer, String>() {
        {
            put(1, "四川");
            put(2, "北京");
            put(3, "上海");
        }
    };


    public static void main(String[] args) throws Exception {
        //countDownDemo();
        //futureTaskDemo();
    }

    /**
     * 使用 CountDownLatch 实现并发获取值
    */
    private static void countDownDemo() throws InterruptedException {
        log.info("开始获取数据");
        CountDownLatch countDownLatch = new CountDownLatch(2);
        ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
        new Thread(()->{
            String message = getMessage(2);
            map.put("message",message);
            countDownLatch.countDown();
        }).start();

        new Thread(()->{
            String address = getAddress(2);
            map.put("address",address);
            countDownLatch.countDown();
        }).start();
        countDownLatch.await();
        insert(map.get("name"), map.get("address"));
    }


    /**
     * 使用 FutureTask 实现并发获取值
     */
    private static void futureTaskDemo() throws InterruptedException, ExecutionException {
        log.info("开始获取数据");
        Callable<String> userCall = new Callable<String>(){

            @Override
            public String call() throws Exception {
                return getMessage(2);
            }
        };
        Callable<String> addressCall = new Callable<String>(){

            @Override
            public String call() throws Exception {
                return getAddress(2);
            }
        };
        FutureTask<String> userFuture = new FutureTask<>(userCall);
        FutureTask<String> addFuture = new FutureTask<>(addressCall);
        new Thread(userFuture).start();
        new Thread(addFuture).start();

        insert(userFuture.get(), addFuture.get());
    }

    private static void insert(String message, String address) {
        log.info("插入用户信息：{}，地址：{}", message, address);
    }

    private static String getMessage(int i) {
        timeSleep(2);
        String s = userMap.get(i);
        log.info("获取用户信息：{}",s);
        return s;
    }

    private static String getAddress(int i) {
        timeSleep(2);
        String address = addressMap.get(i);
        log.info("获取地址信息：{}",address);
        return address;
    }

    private static void timeSleep(Integer seconds) {
        try {
            TimeUnit.SECONDS.sleep(seconds);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}
